package com.treeroot.treenet.protocol.abs.tcp.route;

import com.treeroot.treenet.protocol.abs.tcp.decode.DecodedMessage;
import com.treeroot.treenet.protocol.abs.tcp.session.TcpSession;
import com.treeroot.treenet.protocol.tcp.TcpUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * Description: ping处理程序
 * Create By TreeRoot
 * Create Time: 2025/06/02 10:19
 * Class Name: PingHandler
 *
 * @author TreeRoot
 */
@Slf4j
public class PingHandler implements RouteHandler {
    @Override
    public void handle(DecodedMessage message, TcpSession session) throws IOException {
        log.info("收到 Ping 消息，来自连接: {}", session.getChannelId());
        log.info("收到客户端数据: {}", message.getPayload());
        String resp = "【后端服务响应】：" + message.getPayload().toUpperCase();
        ByteBuffer respBuf = TcpUtil.buildResponse(
                message.getMagic(),
                message.getVersion(),
                message.getEncoding(),
                resp
        );
        session.send(respBuf);
        log.info("有效数据处理完成，已发送响应给客户端");
    }
}